$(window).load(function(){
	var phoneNum;// 定义用户输入手机号

	//点击获取验证码
	$(".get-code").on("click", function(){
		phoneNum = $(".phone").val();
		if(phoneNum == ""){
			warnText($(".black-bg"),"手机号码不允许为空！",1000);
		}else if(regs.phoneReg.test(phoneNum)) {
			getCode(phoneNum);// 后台发送验证码给客户
		}else{
			warnText($(".black-bg"),"请输入11位手机号码！",1000);
		}
	})

	//点击绑定按钮
	
	var messageCode;// 定义短信验证码

	// 获取短信验证码
	function getCode(phoneNum){
		$.ajax({
			type: "GET",
			url: wxAddress+"/weixin/bind/sendCode?mobile="+phoneNum,
			async: true,
			timeout : 1000*60*30,
			contentType: "application/json; charset=utf-8",
			success: function(data) {
				if (data.code == "0000") {
					warnText($(".black-bg"),"已发送验证码！",1000);
					reacquires();// 开启倒计时
					messageCode = data.data;// 短信验证码赋值
				}else {
					warnText($(".black-bg"),"获取验证码失败!",1000);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				console.log("............获取验证码..............");
				console.log("state="+XMLHttpRequest.readyState);
				console.log("status="+XMLHttpRequest.status);
				console.log("errorThrown="+errorThrown);
				console.log("............获取验证码..............");
			}
		});
	}

	// 1分钟倒计时及重新获取按钮
	function reacquires(){
		$(".get-code").off("click");
		phoneNum = $(".phone").val();
		var seconds = 59;
		var timer = setInterval(function(){
			if(seconds == 0){
				$(".get-code").html("重新获取");
				clearInterval(timer);
				$(".get-code").on("click", function(){
					reacquires();
					getCode(phoneNum);
					// 后台发送验证码给客户
				})
			}else{
				$(".get-code").html(seconds+"s后可获取");
				seconds--;
			}
		},1000);
	}

	//点击注册，验证验证码是否一致，本地验证密码格式，成功发送
	$(".sure-bind").on("click", function(){
		var enterCode = $(".code").val().trim();// 用户输入的验证码
		if(messageCode){
			if($(".phone").val() == ""){
				warnText($(".black-bg"),"手机号码不允许为空！",1000);
				return false;
			}else if(phoneNum != $(".phone").val()){
				warnText($(".black-bg"),"手机号码与验证码不匹配！",1000);
				return false;
			}else if(regs.phoneReg.test($(".phone").val()) == false){
				warnText($(".black-bg"),"请输入11位手机号码！",1000);
				return false;
			}
			if(enterCode == ""){
				warnText($(".black-bg"),"验证码为空！",1000);
				return false;
			}else if(enterCode != messageCode){
				warnText($(".black-bg"),"验证码错误！",1000);
				return false;
			}
		}else {
			warnText($(".black-bg"),"请先获取验证码！",1000);
			return false;
		}
		
		$.ajax({
			type: "GET",
			url: wxAddress+"/weixin/bind/verifyCode?mobile="+phoneNum+"&code="+enterCode,
			async:true,
			contentType: "application/json; charset=utf-8",
			timeout : 1000*60*30,
			success: function(data) {
				if (data.code == "0000") {
					warnText($(".black-bg"),"注册成功！",1000);
					locTimer = setTimeout(function(){
						clearInterval(locTimer);
						window.location.href = "bindsuccess.html";
					},2000)
				}else if(data.code == "0006") {
					warnText($(".black-bg"),"注册失败:验证码错误！",1000);
				}else {
					warnText($(".black-bg"),"注册失败:"+data.msg,1000);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				console.log("............POST..............");
				console.log("state="+XMLHttpRequest.readyState);
				console.log("status="+XMLHttpRequest.status);
				console.log("errorThrown="+errorThrown);
				console.log("............POST..............");
			}
		});
	})
})